package screen
{
	import com.greensock.TweenLite;
	
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.utils.setTimeout;
	
	import helper.Resources;
	import helper.SWFResource;
	import helper.ValueObject;

	public class PreLoaderScreen extends Sprite
	{
		private var _preloader_resource:SWFResource;
		private var _onshow_bl:Boolean = false;
		private var _preloader_mc:MovieClip;
		private var _progress_event_s2:ProgressEvent;
		private var _progress_event_s1:ProgressEvent;
		public function PreLoaderScreen():void
		{
			var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			var loader:Loader = new Loader();
			var req:URLRequest = new URLRequest(Resources.PRELOADER_SWF_PATH);
			//loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onAssetsError);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadedPreloader);
			loader.load(req, context);
			
		}
		public function onShow():void{
			if(_preloader_resource){
				init();
			}
			_onshow_bl = true;
			//this.visible = false;
			
		}
		public function onHide():void{
			_onshow_bl = false;
		}
		private function onLoadedPreloader(_event:Event):void{
			var loader3:Loader = LoaderInfo(_event.target).loader;
			_preloader_resource = new SWFResource();
			_preloader_resource.appDomain = loader3.contentLoaderInfo.applicationDomain;
			
			var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			var loader:Loader = new Loader();
			var req:URLRequest = new URLRequest(Resources.SACOMBANK_SWF1_PATH);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgressSacombank1);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadedSacombank1);
			loader.load(req, context);
			
			var context2:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
			var loader2:Loader = new Loader();
			var req2:URLRequest = new URLRequest(Resources.SACOMBANK_SWF2_PATH);
			loader2.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgressSacombank2);
			loader2.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadedSacombank2);
			loader2.load(req2, context2);
			
			if(_onshow_bl){
				init();
			}
		}
		private function init():void{
			ValueObject.WIDTH = Manulife.getInstance().stage.stageWidth;
			ValueObject.HEIGHT = Manulife.getInstance().stage.stageHeight;
			_preloader_mc = _preloader_resource.getExportedAsset("preloader") as MovieClip;
			_preloader_mc.x = ValueObject.WIDTH/2;
			_preloader_mc.y = ValueObject.HEIGHT/2;
			addChild(_preloader_mc);
			start();
			//setTimeout(start,1000);
		}
		private function onLoadedSacombank1(_event:Event):void{
			var loader:Loader = LoaderInfo(_event.target).loader;
			ValueObject.C1_RESOURCE = new SWFResource();
			ValueObject.C1_RESOURCE.appDomain = loader.contentLoaderInfo.applicationDomain;
		}
		private function onProgressSacombank1(e:ProgressEvent):void{
			_progress_event_s1 = e;
		}
		private function onLoadedSacombank2(_event:Event):void{
			var loader:Loader = LoaderInfo(_event.target).loader;
			ValueObject.C2_RESOURCE = new SWFResource();
			ValueObject.C2_RESOURCE.appDomain = loader.contentLoaderInfo.applicationDomain;
		}
		private function onProgressSacombank2(e:ProgressEvent):void{
			_progress_event_s2 = e;
		}
		
		private function start():void{
			this.addEventListener(Event.ENTER_FRAME,showLoader);
		}
		private function showLoader(e:Event):void{
			if(_progress_event_s2 && _progress_event_s1){
				var percent:Number = 100*(_progress_event_s2.bytesLoaded + _progress_event_s1.bytesLoaded)/(_progress_event_s2.bytesTotal + _progress_event_s1.bytesTotal);
				_preloader_mc.loaderMc.gotoAndStop(Math.ceil(percent));
				_preloader_mc.gotoAndStop(Math.ceil(percent));
				TextField(_preloader_mc.valueTf).htmlText = '<font color="#ffffff" size="20">'+percent.toFixed(2) + '%</font>';
				//TextField(_preloader_mc.loaderMc.valueTf).htmlText = '<font color="#ffffff" size="16">'+ "Đang tải game" + '</font>';
				if(percent == 100){
					_preloader_mc.play();
					this.removeEventListener(Event.ENTER_FRAME,showLoader);
					setTimeout(camiScreen,1500);
				}
			}
		}
		private function camiScreen():void{
			this.visible = false;
			while(this.numChildren>0){
				this.removeChildAt(0);
			}
			ManulifeScreen.getInstance().onShow();
		
		}
		
	}
}